package com.RJAI.gemini.utils;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.gson.JsonObject;
import io.opencensus.metrics.export.Distribution;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;

/*
作者信息
WeChat:ciansec
QQ:1068092013
 */
public class GCloudUtils {


    public static HashMap<String,String> generateGCloudJWTByJson(String jsonFile) throws IOException {
        String credentialsPath = jsonFile;
        if (!FileUtil.exist( jsonFile )){
            return null;
        }
        GoogleCredentials credentials = ServiceAccountCredentials
                .fromStream(new FileInputStream(credentialsPath));
        if (credentials.createScopedRequired()) {
            credentials = credentials.createScoped( Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
        }
        HashMap<String, String> map = new HashMap<>();
        String jsonStr = FileUtil.readUtf8String( jsonFile );
        map.put( "project_id", JSONUtil.parseObj( jsonStr ) .getStr( "project_id" ));
        credentials.refreshIfExpired();
        AccessToken accessToken = credentials.getAccessToken();
        map.put( "accessToken",accessToken.getTokenValue() );
        return map;
    }
}
